<?php
namespace app\controllers;


use app\logic\PlayLogic;
use app\common\corelib\LocalUpload;
use yii;

class PlayController extends BackendController
{

    public function actionQuan()
    {
        $query = Yii::$app->request->get('form', []);
        $page = Yii::$app->request->get('page', 1);
        $playLogic = new PlayLogic();
        $this->data['breadcrumbs'] = '入场券管理';
        return $this->render('quan', [
            'play' => $playLogic->getQuan($query, $page),
            'query' => $query
        ]);
    }

    public function actionCard()
    {
        $query = Yii::$app->request->get('form', []);
        $page = Yii::$app->request->get('page', 1);
        $playLogic = new PlayLogic();
        $this->data['breadcrumbs'] = '会员卡管理';
        return $this->render('card', [
            'play' => $playLogic->getCard($query, $page),
            'query' => $query
        ]);
    }
    public function actionEditQuan()
    {
        $playLogic = new PlayLogic();
        if(!Yii::$app->request->post()){
            $id = Yii::$app->request->get('play_id');
            $play = $playLogic->getById($id);
            $this->data['breadcrumbs'] = ['入场券管理' => U('play/quan'), $play['play_name'] => '', '更新' => ''];
            if(file_exists(dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR.'web'.$play['play_pic'])){
                $pic = $play['play_pic'];
            }else{
                $pic = '/default.jpg';
            }
            return $this->render('edit-quan', [
                'play' => $play,
                'pic' => $pic,
                'theme' => '/assets/',
                'actionUrl' => U('play/edit-quan'),
            ]);
        }else{
            $params = Yii::$app->request->post('form');
            $id = Yii::$app->request->post('play_id');
            if($_FILES['pic']['name']){
                LocalUpload::go_upload($_FILES['pic']);
                $uploadresult = '/uploads/'.LocalUpload::$filedir.'/'.LocalUpload::$filename;
                $thumbs = LocalUpload::thumbs(100,100);
            }else{
                $uploadresult = '';
            }

            $logicRes = $playLogic->editQuanById($id, $params,$uploadresult);
            if ($logicRes) {
                Yii::$app->getSession()->setFlash('success','更新入场券成功');
                jump('play/quan');
            } else {
                Yii::$app->getSession()->setFlash('error','更新入场券失败');
                $this->goBack(Yii::$app->request->referrer);
            }
        }

    }

    public function actionEditCard()
    {
        $playLogic = new PlayLogic();
        if(!Yii::$app->request->post()){
            $id = Yii::$app->request->get('play_id');
            $play = $playLogic->getById($id);
            $this->data['breadcrumbs'] = ['会员卡管理' => U('play/card'), $play['play_name'] => '', '更新' => ''];
            if(file_exists(dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR.'web'.$play['play_pic'])){
                $pic = $play['play_pic'];
            }else{
                $pic = '/default.jpg';
            }
            return $this->render('edit-card', [
                'play' => $play,
                'pic' => $pic,
                'theme' => '/assets/',
                'actionUrl' => U('play/edit-card'),
            ]);
        }else{
            $params = Yii::$app->request->post('form');
            $id = Yii::$app->request->post('play_id');
            if($_FILES['pic']['name']){
                LocalUpload::go_upload($_FILES['pic']);
                $uploadresult = '/uploads/'.LocalUpload::$filedir.'/'.LocalUpload::$filename;
                $thumbs = LocalUpload::thumbs(100,100);
            }else{
                $uploadresult = '';
            }

            $logicRes = $playLogic->editCardById($id, $params,$uploadresult);
            if ($logicRes) {
                Yii::$app->getSession()->setFlash('success','更新会员卡成功');
                jump('play/card');
            } else {
                Yii::$app->getSession()->setFlash('error','更新会员卡失败');
                $this->goBack(Yii::$app->request->referrer);
            }
        }

    }


    public function actionSaveQuanStatus()
    {
        $id = Yii::$app->request->get('play_id');
        $playLogic = new PlayLogic();
        $logicRes = $playLogic->saveStatus($id);
        if ($logicRes) {
            Yii::$app->getSession()->setFlash('success','更改入场券状态成功');
        } else {
            Yii::$app->getSession()->setFlash('error','更改入场券状态失败');
        }
        return $this->redirect(U('play/quan'));
    }

    public function actionSaveCardStatus()
    {
        $id = Yii::$app->request->get('play_id');
        $playLogic = new PlayLogic();
        $logicRes = $playLogic->saveStatus($id);
        if ($logicRes) {
            Yii::$app->getSession()->setFlash('success','更改会员卡状态成功');
        } else {
            Yii::$app->getSession()->setFlash('error','更改会员卡状态失败');
        }
        return $this->redirect(U('play/card'));
    }

    public function actionAddQuan()
    {
        if(!Yii::$app->request->post()){
            $this->data['breadcrumbs'] = ['入场券管理' => U('play/quan'), ' 添加入场券' => ''];
            return $this->render('add-quan', [
                'theme' => '/assets/',
                'actionUrl' => U('play/add-quan')
            ]);
        }else{
            $params = Yii::$app->request->post('form');
            $playLogic = new PlayLogic();
            if($_FILES['pic']['name']){
                LocalUpload::go_upload($_FILES['pic']);
                $uploadresult = '/uploads/'.LocalUpload::$filedir.'/'.LocalUpload::$filename;
                $thumbs = LocalUpload::thumbs(100,100);
            }else{
                $uploadresult = '';
            }
            $logicRes = $playLogic->addQuan($params,$uploadresult);
            if ($logicRes) {
                Yii::$app->getSession()->setFlash('success','新增入场券成功');
                jump('play/quan');
            } else {
                Yii::$app->getSession()->setFlash('error','新增入场券失败');
                $this->goBack(Yii::$app->request->referrer);
            }
        }
    }

    public function actionAddCard()
    {
        if(!Yii::$app->request->post()){
            $this->data['breadcrumbs'] = ['会员卡管理' => U('play/card'), ' 添加会员卡' => ''];
            return $this->render('add-card', [
                'theme' => '/assets/',
                'actionUrl' => U('play/add-card')
            ]);
        }else{
            $params = Yii::$app->request->post('form');
            $playLogic = new PlayLogic();
            if($_FILES['pic']['name']){
                LocalUpload::go_upload($_FILES['pic']);
                $uploadresult = '/uploads/'.LocalUpload::$filedir.'/'.LocalUpload::$filename;
                $thumbs = LocalUpload::thumbs(100,100);
            }else{
                $uploadresult = '';
            }
            $logicRes = $playLogic->addCard($params,$uploadresult);
            if ($logicRes) {
                Yii::$app->getSession()->setFlash('success','新增会员卡成功');
                jump('play/card');
            } else {
                Yii::$app->getSession()->setFlash('error','新增会员卡失败');
                $this->goBack(Yii::$app->request->referrer);
            }
        }
    }




}